package com.flyme.module.file.provider.controller;


import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.flyme.common.core.constants.SettingConstant;
import com.flyme.common.core.model.ResultBody;
import com.flyme.module.file.client.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @author flyme
 */
@Slf4j
@RestController
@Api(description="基本配置接口")
@RequestMapping("/setting")
public class SysSettingController{

    @Autowired
    private StringRedisTemplate redisTemplate;

    @RequestMapping(value="/seeSecret/{settingName}", method=RequestMethod.GET)
    @ApiOperation(value="查看私密配置")
    public ResultBody<Object> seeSecret(@PathVariable String settingName){

        String result="", v;
        if(settingName.equals(SettingConstant.QINIU_OSS)){
            // 七牛云
            v=redisTemplate.opsForValue().get(SettingConstant.QINIU_OSS);
            if(StrUtil.isNotBlank(v)){
                result=JSONUtil.toBean(v,OssSetting.class).getSecretKey();
            }
        }else if(settingName.equals(SettingConstant.ALI_OSS)){
            // 阿里云OSS
            v=redisTemplate.opsForValue().get(SettingConstant.ALI_OSS);
            if(StrUtil.isNotBlank(v)){
                result=JSONUtil.toBean(v,OssSetting.class).getSecretKey();
            }
        }else if(settingName.equals(SettingConstant.TENCENT_OSS)){
            // 腾讯云COS
            v=redisTemplate.opsForValue().get(SettingConstant.TENCENT_OSS);
            if(StrUtil.isNotBlank(v)){
                result=JSONUtil.toBean(v,OssSetting.class).getSecretKey();
            }
        }else if(settingName.equals(SettingConstant.ALI_SMS)){
            // 阿里云短信
            v=redisTemplate.opsForValue().get(SettingConstant.ALI_SMS);
            if(StrUtil.isNotBlank(v)){
                result=JSONUtil.toBean(v,SmsSetting.class).getSecretKey();
            }
        }else if(settingName.equals(SettingConstant.EMAIL_SETTING)){
            // 邮件
            v=redisTemplate.opsForValue().get(SettingConstant.EMAIL_SETTING);
            if(StrUtil.isNotBlank(v)){
                result=JSONUtil.toBean(v,EmailSetting.class).getPassword();
            }
        }else if(settingName.equals(SettingConstant.VAPTCHA_SETTING)){
            // vaptcha验证码
            v=redisTemplate.opsForValue().get(SettingConstant.VAPTCHA_SETTING);
            if(StrUtil.isNotBlank(v)){
                result=JSONUtil.toBean(v,VaptchaSetting.class).getSecretKey();
            }
        }
        return ResultBody.success().setData(result);
    }

    @RequestMapping(value="/oss/check", method=RequestMethod.GET)
    @ApiOperation(value="检查OSS配置")
    public ResultBody<Object> osscheck(){

        String used=redisTemplate.opsForValue().get(SettingConstant.OSS_USED);
        if(StrUtil.isBlank(used)){
            return ResultBody.failed(501,"您还未配置第三方OSS服务");
        }
        return ResultBody.success().setData(used);
    }

    @RequestMapping(value="/oss/{serviceName}", method=RequestMethod.GET)
    @ApiOperation(value="查看OSS配置")
    public ResultBody<OssSetting> oss(@PathVariable String serviceName){

        String v="";
        if(serviceName.equals(SettingConstant.QINIU_OSS)){
            v=redisTemplate.opsForValue().get(SettingConstant.QINIU_OSS);
        }else if(serviceName.equals(SettingConstant.ALI_OSS)){
            v=redisTemplate.opsForValue().get(SettingConstant.ALI_OSS);
        }else if(serviceName.equals(SettingConstant.TENCENT_OSS)){
            v=redisTemplate.opsForValue().get(SettingConstant.TENCENT_OSS);
        }else if(serviceName.equals(SettingConstant.LOCAL_OSS)){
            v=redisTemplate.opsForValue().get(SettingConstant.LOCAL_OSS);
        }
        if(StrUtil.isBlank(v)){
            return ResultBody.success();
        }
        OssSetting ossSetting=JSONUtil.toBean(v,OssSetting.class);
        ossSetting.setSecretKey("**********");
        return ResultBody.success().setData(ossSetting);
    }

    @RequestMapping(value="/sms/{serviceName}", method=RequestMethod.GET)
    @ApiOperation(value="查看短信配置")
    public ResultBody<SmsSetting> sms(@PathVariable String serviceName){

        String v="";
        if(serviceName.equals(SettingConstant.ALI_SMS)){
            v=redisTemplate.opsForValue().get(SettingConstant.ALI_SMS);
        }
        if(StrUtil.isBlank(v)){
            return ResultBody.success();
        }
        SmsSetting smsSetting=JSONUtil.toBean(v,SmsSetting.class);
        smsSetting.setSecretKey("**********");
        if(smsSetting.getType()!=null){
            String code=redisTemplate.opsForValue().get(getSmsType(smsSetting.getType()));
            smsSetting.setTemplateCode(code);
        }
        return ResultBody.success().setData(smsSetting);
    }

    @RequestMapping(value="/vaptcha", method=RequestMethod.GET)
    @ApiOperation(value="查看vaptcha配置")
    public ResultBody<VaptchaSetting> vaptcha(){

        String v=redisTemplate.opsForValue().get(SettingConstant.VAPTCHA_SETTING);
        if(StrUtil.isBlank(v)){
            return ResultBody.success();
        }
        VaptchaSetting vaptchaSetting=JSONUtil.toBean(v,VaptchaSetting.class);
        vaptchaSetting.setSecretKey("**********");
        return ResultBody.success().setData(vaptchaSetting);
    }

    @RequestMapping(value="/email", method=RequestMethod.GET)
    @ApiOperation(value="查看email配置")
    public ResultBody<EmailSetting> email(){

        String v=redisTemplate.opsForValue().get(SettingConstant.EMAIL_SETTING);
        if(StrUtil.isBlank(v)){
            return ResultBody.success();
        }
        EmailSetting emailSetting=JSONUtil.toBean(v,EmailSetting.class);
        emailSetting.setPassword("**********");
        return ResultBody.success().setData(emailSetting);
    }

    @RequestMapping(value="/other", method=RequestMethod.GET)
    @ApiOperation(value="查看其他配置")
    public ResultBody<OtherSetting> other(){

        String v=redisTemplate.opsForValue().get(SettingConstant.OTHER_SETTING);
        if(StrUtil.isBlank(v)){
            return ResultBody.success();
        }
        OtherSetting otherSetting=JSONUtil.toBean(v,OtherSetting.class);
        return ResultBody.success().setData(otherSetting);
    }

    @RequestMapping(value="/sms/templateCode/{type}", method=RequestMethod.GET)
    @ApiOperation(value="查看短信模板配置")
    public ResultBody<String> smsTemplateCode(@PathVariable Integer type){

        String templateCode="";
        if(type!=null){
            String template=getSmsType(type);
            templateCode=redisTemplate.opsForValue().get(template);
        }
        return ResultBody.success().setData(templateCode);
    }

    @RequestMapping(value="/oss/set", method=RequestMethod.POST)
    @ApiOperation(value="OSS配置")
    public ResultBody<Object> ossSet(@ModelAttribute OssSetting ossSetting){

        if(ossSetting.getServiceName().equals(SettingConstant.QINIU_OSS)){
            // 七牛
            String v=redisTemplate.opsForValue().get(SettingConstant.QINIU_OSS);
            if(StrUtil.isNotBlank(v)&&!ossSetting.getChanged()){
                String secrectKey=JSONUtil.toBean(v,OssSetting.class).getSecretKey();
                ossSetting.setSecretKey(secrectKey);
            }
            redisTemplate.opsForValue().set(SettingConstant.QINIU_OSS,JSONUtil.toJsonStr(ossSetting));
            redisTemplate.opsForValue().set(SettingConstant.OSS_USED,SettingConstant.QINIU_OSS);
        }else if(ossSetting.getServiceName().equals(SettingConstant.ALI_OSS)){
            // 阿里
            String v=redisTemplate.opsForValue().get(SettingConstant.ALI_OSS);
            if(StrUtil.isNotBlank(v)&&!ossSetting.getChanged()){
                String secrectKey=JSONUtil.toBean(v,OssSetting.class).getSecretKey();
                ossSetting.setSecretKey(secrectKey);
            }
            redisTemplate.opsForValue().set(SettingConstant.ALI_OSS,JSONUtil.toJsonStr(ossSetting));
            redisTemplate.opsForValue().set(SettingConstant.OSS_USED,SettingConstant.ALI_OSS);
        }else if(ossSetting.getServiceName().equals(SettingConstant.TENCENT_OSS)){
            // 腾讯
            String v=redisTemplate.opsForValue().get(SettingConstant.TENCENT_OSS);
            if(StrUtil.isNotBlank(v)&&!ossSetting.getChanged()){
                String secrectKey=JSONUtil.toBean(v,OssSetting.class).getSecretKey();
                ossSetting.setSecretKey(secrectKey);
            }
            redisTemplate.opsForValue().set(SettingConstant.TENCENT_OSS,JSONUtil.toJsonStr(ossSetting));
            redisTemplate.opsForValue().set(SettingConstant.OSS_USED,SettingConstant.TENCENT_OSS);
        }else if(ossSetting.getServiceName().equals(SettingConstant.LOCAL_OSS)){
            // 本地
            redisTemplate.opsForValue().set(SettingConstant.LOCAL_OSS,JSONUtil.toJsonStr(ossSetting));
            redisTemplate.opsForValue().set(SettingConstant.OSS_USED,SettingConstant.LOCAL_OSS);
        }
        return ResultBody.success();
    }

    @RequestMapping(value="/sms/set", method=RequestMethod.POST)
    @ApiOperation(value="短信配置")
    public ResultBody<Object> smsSet(@ModelAttribute SmsSetting smsSetting){

        if(smsSetting.getServiceName().equals(SettingConstant.ALI_SMS)){
            // 阿里
            String v=redisTemplate.opsForValue().get(SettingConstant.ALI_SMS);
            if(StrUtil.isNotBlank(v)&&!smsSetting.getChanged()){
                String secrectKey=JSONUtil.toBean(v,SmsSetting.class).getSecretKey();
                smsSetting.setSecretKey(secrectKey);
            }
            if(smsSetting.getType()!=null){
                String type=getSmsType(smsSetting.getType());
                redisTemplate.opsForValue().set(type,smsSetting.getTemplateCode());
            }
            smsSetting.setType(null);
            smsSetting.setTemplateCode(null);
            redisTemplate.opsForValue().set(SettingConstant.ALI_SMS,JSONUtil.toJsonStr(smsSetting));
            redisTemplate.opsForValue().set(SettingConstant.SMS_USED,SettingConstant.ALI_SMS);
        }
        return ResultBody.success();
    }

    @RequestMapping(value="/email/set", method=RequestMethod.POST)
    @ApiOperation(value="email配置")
    public ResultBody<Object> emailSet(@ModelAttribute EmailSetting emailSetting){

        String v=redisTemplate.opsForValue().get(SettingConstant.EMAIL_SETTING);
        if(StrUtil.isNotBlank(v)&&!emailSetting.getChanged()){
            String password=JSONUtil.toBean(v,EmailSetting.class).getPassword();
            emailSetting.setPassword(password);
        }
        redisTemplate.opsForValue().set(SettingConstant.EMAIL_SETTING,JSONUtil.toJsonStr(emailSetting));
        return ResultBody.success();
    }

    @RequestMapping(value="/vaptcha/set", method=RequestMethod.POST)
    @ApiOperation(value="vaptcha配置")
    public ResultBody<Object> vaptchaSet(@ModelAttribute VaptchaSetting vaptchaSetting){

        String v=redisTemplate.opsForValue().get(SettingConstant.VAPTCHA_SETTING);
        if(StrUtil.isNotBlank(v)&&!vaptchaSetting.getChanged()){
            String key=JSONUtil.toBean(v,VaptchaSetting.class).getSecretKey();
            vaptchaSetting.setSecretKey(key);
        }
        redisTemplate.opsForValue().set(SettingConstant.VAPTCHA_SETTING,JSONUtil.toJsonStr(vaptchaSetting));
        return ResultBody.success();
    }

    @RequestMapping(value="/other/set", method=RequestMethod.POST)
    @ApiOperation(value="其他配置")
    public ResultBody<Object> otherSet(@ModelAttribute OtherSetting otherSetting){

        String v=redisTemplate.opsForValue().get(SettingConstant.OTHER_SETTING);
        redisTemplate.opsForValue().set(SettingConstant.OTHER_SETTING,JSONUtil.toJsonStr(otherSetting));
        return ResultBody.success();
    }

    public String getSmsType(Integer type){
        switch(type){
            case -1:
                return SettingConstant.ALI_SMS_COMMON;
            case 0:
                return SettingConstant.ALI_SMS_REGIST;
            case 1:
                return SettingConstant.ALI_SMS_LOGIN;
            case 2:
                return SettingConstant.ALI_SMS_CHANGE_MOBILE;
            case 3:
                return SettingConstant.ALI_SMS_CHANG_PASS;
            case 4:
                return SettingConstant.ALI_SMS_RESET_PASS;
            default:
                return SettingConstant.ALI_SMS_COMMON;
        }
    }
}
